//
//  MediaManager.m
//  DongYu
//
//  Created by DongYu  on 2023.
//  Copyright © 2023 DongYu . All rights reserved.
//

#import "MediaManager.h"

@implementation MediaManager

+ (instancetype)shareInstance {
    static MediaManager *_manager = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

//请求相册权限
- (void)requestPhotoAuthorizationStatus:(void (^)(BOOL granted))completion {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {
        //系统还未知是否访问，第一次开启相册时
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {

                dispatch_async(dispatch_get_main_queue(), ^{
                                        
                    if (completion) {
                        completion(YES);
                    }
                });
            }
        }];
        
    } else if (status == PHAuthorizationStatusAuthorized) {//已经授权
        if (completion) {
            completion(YES);
        }
    }  else {
        if (completion) {
            completion(NO);
        }
    }
}

//打开相机权限
- (void)requestCameraAuthorizationStatus:(void (^)(BOOL granted))completion {
    //判断是否有相机权限
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//读取设备授权状态
    if (status == AVAuthorizationStatusNotDetermined) {
        //系统还未知是否访问，第一次开启相机时
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (granted) {
                    if (completion) {
                        completion(YES);
                    }
                }
            });
        }];
    } else if (status == AVAuthorizationStatusAuthorized) {//已经授权
        if (completion) {
            completion(YES);
        }
    } else {
        //拒绝
        if (completion) {
            completion(NO);
        }
    }
}


- (void)requestMaikeAuthorizationStatus:(void (^)(BOOL granted))completion {
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (status == AVAuthorizationStatusNotDetermined) {
        //系统还未知是否访问，第一次

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                                    
                if (completion) {
                    completion(granted);
                }
            });
        }];
        
    } else if (status == AVAuthorizationStatusAuthorized) {//已经授权
        
        if (completion) {
            completion(YES);
        }
        
    } else {
        if (completion) {
            completion(NO);
        }
        
    }
}

#pragma mark - other
//提示用户进行麦克风使用授权
- (void)showSetAlertView {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用麦克风权限，才能为你提供调音校准服务" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //跳入当前App设置界面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:setAction];
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
    
}

//提示没有相册权限
- (void)showAlertViewAboutNotAuthorAccessPhoto {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用相册权限，才能将图片保存到相册中" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                    
                }];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [kAppDelegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    
}

//提示没有相机权限
- (void)showAlertViewAboutNotAuthorAccessCamera {
    if ([NSLocalizedString(@"Language",nil) isEqualToString:@"EN"]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Tips" message:@"You need to agree to use camera permissions in order to take photos and use the translation function for you" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Set" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                    
                }];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [kAppDelegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    } else {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要你同意使用相机权限，才能为你拍照使用翻译功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                    
                }];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [kAppDelegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    }
}

@end
